﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VehicleMaintenance.Framework;
using VehicleMaintenance.Models;

namespace VehicleMaintenance.Systems
{
	public partial class ChangePwdForm : BaseForm
	{
		public ChangePwdForm()
		{
			InitializeComponent();
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			try
			{
				this.Close();
			}
			catch (Exception ex)
			{
				this.ShowInfo(ex.Message);
			}
		}

		private void btnSave_Click(object sender, EventArgs e)
		{
			try
			{
				errorProvider1.Clear();
				if (txtOldPwd.Text.Length == 0)
				{
					errorProvider1.SetError(txtOldPwd,"请输入旧密码");
					txtOldPwd.Focus();
					return;
				}
				if (MainForm2.User.PassWord != txtOldPwd.Text)
				{
					errorProvider1.SetError(txtOldPwd, "旧密码输入错误");
					txtOldPwd.Focus();
					return;
				}
				if (txtNewPwd.Text.Length == 0)
				{
					errorProvider1.SetError(txtNewPwd, "请输入新密码");
					txtNewPwd.Focus();
					return;
				}
				if (txtConfirmPwd.Text != txtNewPwd.Text)
				{
					errorProvider1.SetError(txtConfirmPwd, "两次输入的密码不一致");
					txtConfirmPwd.Focus();
					return;
				}
				CarDBContext db = new CarDBContext();
				User user = db.Users.First(p=>p.UserID == MainForm2.User.UserID);
				user.PassWord = txtNewPwd.Text;
				db.SaveChanges();
				MainForm2.User = user;
				this.ShowInfo("密码修改成功");
				this.DialogResult = System.Windows.Forms.DialogResult.OK;
			}
			catch (Exception ex)
			{
				this.ShowInfo(ex.Message);
			}
		}
	}
}
